package com.jd.webapp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jd.common.convention.TTResult;
import com.jd.goods.pojo.Goods;
import com.jd.goods.service.GoodsService;

@Controller
public class GoodsController {
	@Autowired
	private GoodsService goodsService;
	
	@RequestMapping("/item/{id}")
	public @ResponseBody Object getItemById(@PathVariable Long id){
		try {
			return goodsService.getGoodsById(id);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@RequestMapping("/item/list")
	public @ResponseBody Object itemList(int page,int rows){
		try {
			List<Goods> list = goodsService.getList(page, rows);
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("total", goodsService.getCount());
			map.put("rows", list);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@RequestMapping("/item/save")
	public @ResponseBody Object addItem(Goods goods,String desc,String itemParams){
		try {
			if(goodsService.tx_addGoods(goods, desc, itemParams)==1){
				return TTResult.ok();
			}else{
				return TTResult.fail();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return TTResult.fail();
		}
	}
}
